//提供接口
module.exports = {
  getUserKey : getUserKey,//保存登录的用户信息
  getOpenPwKey : getOpenPwKey,//保存开门的钥匙
  getUrl:getUrl,//host接口
  postData:postData,
  getData:getData,
  getUserId:getUserId,
  getUser:getUser
}

//post===============
//url==网址 ,param===参数,back===返回的函数
function postData(data) {
  var url = data.url;
  var param = data.param;
  var back = data.back;
  console.log(url+'++++'+param.userId);
  wx.request({
    url: getUrl()+url,
    data: param,
    method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    header: {
              'content-type': 'application/x-www-form-urlencoded'
          },
   complete: function(res) {
           wx.hideLoading();
  },
   fail: function(res) {
       wx.showToast({
              title: '请求错误',
              icon: 'error',
              mask: true,
          });
      back(false);
  },
  success: function(res) {
      console.log(res);
      wx.hideLoading();
      if(res.data.status==500){
          wx.showToast({
              title: '请求成功',
              icon: 'success',
              mask: true,
          });
          back(res.data.data);
          return;
      }else if(res.data.length>0){
          wx.showToast({
              title: res.data.data,
              icon: 'error',
              mask: true,
          });
      }
      back(false);
  }
})
}
//get===============
//url==网址 ,param===参数,back===返回的函数
function getData(data) {
  var url = data.url;
  var param = data.param;
  var back = data.back;
  console.log(url+'++++'+param);
  wx.request({
    url: getUrl()+url,
    data: param,
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    
    success: function(res) {
      console.log(res);
      wx.hideLoading();
      if(res.data.status==500){
          wx.showToast({
              title: '请求成功',
              icon: 'success',
              mask: true,
          });
          back(res.data.data);
      }else {
          wx.showToast({
              title: res.data.data,
              icon: 'error',
              mask: true,
          });
      }
      // return res.data.data;
      back(false);
  },
    fail: function(res) {
      // fail
      back(false);
    },
    complete: function(res) {
      // complete
       wx.hideLoading();
    }
  })
}


//接口URL==============
function getUrl(){
  // return "http://120.55.117.108:8989";
  return "http://120.55.117.108:8989";
}

// 获取常用信息==================
//获取userid
function getUserId(data) {
  wx.getStorage({
      key: getUserKey(),//'userInfo',
      fail:function(res) {
          data.back(false);
      //    return false;
      },
      success: function(res) {
          var userData = res.data;
          console.log(userData);
          data.back(userData.usrid);
          // return userData.usrid;
        } 
  });
}
//获取user的登录信息
function getUser(data) {
  wx.getStorage({
      key: getUserKey(),//'userInfo',
      fail:function(res) {
           data.back(false);
      //    return false;
      },
      success: function(res) {
          var userData = res.data;
          console.log(userData);
           data.back(userData);
          // return userData;
        } 
  });
}


//本地保存数据的key==============
//保存登录的用户信息
function getUserKey(){
  return "userInfo";
}
//保存开门的钥匙
function getOpenPwKey() {
  return "openpw";
}
